<?php
/**
* author: lijj
* time：2016-3-15
* function: 随堂测试
*/
class ClassTestAction extends Action {
    public function getOpenId(){
        return session('?openId') ? session('openId') : $this->error('请重新获取该页面');
    }
    public function index() {
        $TEAQUIZ = D('Teacher/TeacherQuiz');
        $openId = $this->getOpenId();
        $classInfo = $TEAQUIZ->where('openId="'.$openId.'"')->select();
        $this->assign('classInfo',$classInfo);
        $this->display();
    }
    public function choiceMake(){
        $this->display();
    }
    // 显示要选择的20道题目
    public function choiceShow(){
        /*============定义一些变量=======*/
        $chapter = I('chapter')?:$this->error('你访问的界面不存在');
        $QUES = M('Questionbank');
        $QuesItem = $QUES->where('type="'.$chapter.'"')->select();
        $this->assign('QuesItem',$QuesItem)->display();
    }
    public function showQuesAll(){
        $quesid = I('quesid')?:$this->error('你访问的界面不存在');
        $QUES = M('Questionbank');
        $QuesItem = $QUES->where('id="'.$quesid.'"')->find();
        $this->assign('QuesItem',$QuesItem)->display();
        // p($QuesItem);
    }
    public function submitQues(){
        $openId = $this->getOpenId();
        $TEACLASS = D('Teacher/TeacherClasses');
        if(!$TEACLASS->where('openId="'.$openId.'"')->select())
            $this->error('你还没有添加班级，请在我的课程里添加班级');
        $class = $TEACLASS->where('openId="'.$openId.'"')->select();
        $this->assign('banji',$class)->display();
    }
    public function handleSubmitQues(){
        if(!IS_AJAX)
            $this->error('你访问的页面不存在');
        $TEACLASS = D('Teacher/TeacherClasses');
        $openId = session('openId');
        $name = D('Teacher/TeacherInfo')->getName($openId);
        $questionId = I('questionId');
        $quesIdAll = explode('_', $questionId);  //获得的测试题目quesid
        $onClass = I('onClass');
        $onClassAll = explode('_', $onClass);
        $QUES = M('questionbank');
        $unit = $QUES->where('id="'.$quesIdAll[0].'"')->getField('type');  //测试题的单元
        $quizName = I('testName');

        /*=========添加随堂测试==============*/
        $quiz = array(
            'openId'   => $openId,
            'name'     => $name,
            'quizName' => $quizName,
            'quizUnit' => $unit,
            'time'     => date('Y-m-d H:i:s')
            );
        D('Teacher/TeacherQuiz')->data($quiz)->add();

         /*=========保存设置的题目==============*/
        for($i=0;$i<count($quesIdAll)-1;$i++){
            $quesInfo = array(
                'primaryId'        =>$quesIdAll[$i],
                'openId'           => $openId,
                'name'             => $name,
                'quizName'         => $quizName,
                'questionPicPath'  => $QUES->where('id="'.$quesIdAll[$i].'"')->getField('questionPicPath'),
                'questionPicName'  => $QUES->where('id="'.$quesIdAll[$i].'"')->getField('questionPicName'), 
                'rightAnswer'      => $QUES->where('id="'.$quesIdAll[$i].'"')->getField('rightAnswer'),
                'analysisPicPath'  => $QUES->where('id="'.$quesIdAll[$i].'"')->getField('analysisPicPath'),
                'analysisPicName'  => $QUES->where('id="'.$quesIdAll[$i].'"')->getField('analysisPicName'),
                'time' => date('Y-m-d H:i:s') );
            D('Teacher/TeacherQuizQuestionbank')->data($quesInfo)->add();
        }
        /*=========试题状态默认设置关闭==============*/
        $classInfo = $TEACLASS->where('openId="'.$openId.'"')->select();
        $classNum = $TEACLASS->where('openId="'.$openId.'"')->count();
        for($i = 0 ; $i < $classNum ; $i ++){
            $quizState = array(
                'openId' => $openId,
                'name' => D('Teacher/TeacherInfo')->getName($openId),
                'quizName' => $quizName,
                'class' => $classInfo[$i]['class'],
                'state' => "关闭"
                );
            D('Teacher/TeacherQuizState')->add($quizState);
        }
        /*=========根据用户的设置将试题状态设置为开启============*/
        for($i=0;$i<count($onClassAll)-1;$i++){
            D('Teacher/TeacherQuizState')->where('class="'.$onClassAll[$i].'"')->save(array('state'=>"开启"));
        }
        $this->ajaxReturn(array('status' => 1),'json');
    }
    //添加试题
    public function addTestQuestion(){
        /*=========定义一些变量========*/
        $TEA = D('Teacher/TeacherInfo');
        $QUES = D('Admin/Questionbank');
        $TEAQUES = D('Teacher/TeacherQuizQuestionbank');
        $TEAQUIZ = D('Teacher/TeacherQuiz');
        $TEAQUIZSTATE = D('Teacher/TeacherQuizState');
        $TEACLASS = D('Teacher/TeacherClasses');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取该页面');
        // $num = isset(I('num')) ? I('num') : $this->error('你输入的数字不存在');
        // $section = empty(I('section')) ? I('section') : "" ;
        //第一次插入数据

        $num = I('num');
        $section = I('section');
        $quizName = $this->getQuziName($section);
        //根据老师题库里有误题目来判断是插入还是查看
        if(!$TEAQUIZ->where('quizName="'.$quizName.'" AND openId = "'.$openId.'"')->find()){
            //插入
            $min = $QUES->where('type="'.$section.'"')->min('id');
            $max = $QUES->where('type="'.$section.'"')->max('id');
            $number = range($min, $max);  //生成一个从$min到$max的数组
            shuffle($number);   //打乱数组
            $data = array_slice($number,0,$num); //生成$num个随机数组
            foreach ($data as $key => $value) {
                $item[$key] = $QUES->where('id='.$data[$key])->find();
                $item[$key]['openId'] = $openId;
                $item[$key]['name'] = $TEA->getName($openId);
                $item[$key]['quizName'] = $quizName;
                $item[$key]['primaryId'] = $QUES->where('id='.$data[$key])->getField('id');
                $item[$key]['id'] = NULL;
                /*=========插入数据库==============*/
                $TEAQUES->data($item[$key])->add();
            }
            /*=========插入随堂测试名称表==============*/

            $quizInfo = array(
                'openId' => $openId,
                'name' => $TEA->getName($openId),
                'quizName' => $quizName,
                'time' => date("Y-m-d H-i-s")
                );
            $TEAQUIZ->add($quizInfo);

            /*=========试题状态默认设置为关闭==============*/
            $classInfo = $TEACLASS->where('openId="'.$openId.'"')->select();
            $classNum = $TEACLASS->where('openId="'.$openId.'"')->count();
            for($i = 0 ; $i < $classNum ; $i ++){
                $quizState = array(
                    'openId' => $openId,
                    'name' => $TEA->getName($openId),
                    'quizName' => $quizName,
                    'class' => $classInfo[$i]['class'],
                    'state' => "关闭"
                    );
                $TEAQUIZSTATE->add($quizState);
            }
            
        }else{
            $item = $TEAQUES->where('quizName="'.$quizName.'"')->select();
        }
        /*======读取随机试题======*/
        // p($item);    
        $this->assign('item',$item);
        $this->display();
    }

    //添加试题
    public function setTestState(){
         /*=========定义一些变量========*/
        $TEA = D('Teacher/TeacherInfo');
        $TEAQUIZ = D('Teacher/TeacherQuiz');
        $TEAQUIZSTATE = D('Teacher/TeacherQuizState');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取该页面');
        $stateInfo = $TEAQUIZSTATE->where('openId="'.$openId.'"')->select();
        $this->assign('stateInfo',$stateInfo);
        // p($stateInfo);
    	$this->display();
    }

    public function handleTestState(){
        if(!IS_AJAX)
            $this->error('访问的地址不存在');
        /*======设置一些变量========*/
        $TEA = D('Teacher/TeacherInfo');
        $TEAQUIZSTATE = D('Teacher/TeacherQuizState');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取该页面');
        $quizState = array(
                    'openId' => $openId,
                    'name' => $TEA->getName($openId),
                    'quizName' => I('quizName'),
                    'class' => I('banji'),
                    'state' => I('state'),
                );
        $TEAQUIZSTATE->where('openId = "'.$openId.'" AND quizName = "'.I('quizName').'" AND class="'.I('banji').'"')->save($quizState);
    }

    /*=======查看未提交名单========*/
    public function showUnsubmit(){
        $this->display();
    }

    /*=======查看答题情况========*/
    public function showAnserCase(){
        $this->display();
    }

    //unit1 与 第一章的转化
    public function getQuziName($section){
        switch ($section) {
            case 'unit1':
                return '第一章';
                break;
            case 'unit2':
                return '第二章';
                break;
            case 'unit3':
                return '第三章';
                break;
            case 'unit4':
                return '第四章';
                break;
            case 'unit5':
                return '第五章';
                break;
            case 'unit6':
                return '第六章';
                break;
            case 'unit7':
                return '第七章';
                break;
            case 'unit8':
                return '第八章' ;
                break;
            default:
                return null;
                break;
        }
    }
}

?>